/*
 * Copyright Eduardo Aponte 2013
 * aponteeduardo@gmail.com
 */
#include <eyelink.h>
#include "mex.h"
#include "utils.h"


void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
    mwSize td[2] = {1,1};
    double *status;
    int bs;
    ELINKNODE eln;
    INT16 r;    
    
    /* Verify input arguments. */
    if(nrhs != 1 ) {
        mexErrMsgTxt("Incorrect number of inputs.");
    } else if( nlhs > 2 ) {
        mexErrMsgTxt("Too many output arguments.");
    }
    
    if ( isscalar( prhs[0] ) ){
        INT16 scalar;
        if ( returnscalar( prhs[0], &bs ) != 1 )
        mexErrMsgTxt("Complex data is not supported.");                        
    } else {
        mexErrMsgTxt("resp should be a scalar.");    
    }
    
    plhs[0] = mxCreateNumericArray( 2, td, mxDOUBLE_CLASS, mxREAL);
    status  = mxGetPr(plhs[0]);
    status[0] = (double ) eyelink_get_node( (INT16 ) bs, &eln );
    
    if (nlhs == 2) {
        int i;
        char nr[ELINKADDRSIZE];
        const char *is[] = {"elinkaddr","name"};
        
        plhs[1] = mxCreateStructMatrix(1,1,2,is);
    
    
        for (i = 0; i < ELINKADDRSIZE; i++ )
            nr[i] = (char ) eln.addr[i];
        
        mxSetField(plhs[1],0,is[0],mxCreateString( nr ));
        mxSetField(plhs[1],0,is[1],mxCreateString( eln.name ));
    }
    
    return;
}
